/*
 * $Id: TestCaseCategoryServlet.java 14 2012-01-10 07:13:46Z rook.alex@gmail.com $
 */
package ua.org.energy.vkek.site.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.org.energy.vkek.site.model.ArticleCategory;
import ua.org.energy.vkek.site.server.CategoryUtils;
import ua.org.energy.vkek.site.server.DataIOException;
import ua.org.energy.vkek.site.server.EMF;

public class TestCaseCategoryServlet extends BaseJsonServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        if (request.getParameter("ArticleCategory") != null) {
            insert(request.getParameter("ArticleCategory"));
        }

        if (request.getParameter("newcategory") != null) {
            update(request.getParameter("newcategory"), request.getParameter("oldcategory"));
        }

        if (request.getParameter("delcategory") != null) {
            delete(request.getParameter("delcategory"));
        }
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            printHead(out);

            out.println("<div id=\"wrap\">");
            out.println("<div id=\"blockform\">");
            // add ArticleCategory
            printForm(out, "add ArticleCategory", new String[][]{{"ArticleCategory name", "ArticleCategory"}});
            //update ArticleCategory
            printForm(out, "update ArticleCategory", new String[][]{{"oldcategory name", "oldcategory"}, {"newcategory name", "newcategory"}});
            //delete ArticleCategory
            printForm(out, "delete ArticleCategory", new String[][]{{"ArticleCategory for del", "delcategory"}});
            out.println("</div>");
            //list all categories
            out.println("<div id=\"list\">");
            printCategoryList(out);
            out.println("</div>");
            out.println("</div>");

            printFooter(out);

        } finally {
            out.close();
        }
    }

    private void printHead(PrintWriter out) {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
        out.println("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
        out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"ru\" xml:lang=\"ru\">");
        out.println("<head>");
        out.println("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />");
        out.println("<title>testcase XHTML</title>");
        out.println("<link rel=\"stylesheet\" href=\"style/testcases.css\"/>");
        out.println("</head>");
        out.println("<body>");

    }

    private void printFooter(PrintWriter out) {
        out.println("</body>");
        out.println("</html>");
    }

    private void printCategoryList(PrintWriter out) {
        List<ArticleCategory> list = getListAll();

        for (ArticleCategory cat : list) {
            out.println("<p>");
            out.println(cat.getCategoryName());
            out.println(cat.getIdent());
            out.println("</p>");
        }

    }

    private void printForm(PrintWriter out, String formname, String[][] inputs) {
        out.println("<div>");
        out.println("<form name=\"" + formname + "\" method=\"get\"");
        out.println("<h3>" + formname + "</h3>");
        for (int i = 0; i < inputs.length; i++) {
            out.println("<p>");
            out.println(inputs[i][0]);
            out.println("<input name=\"" + inputs[i][1] + "\"/>");
            out.println("</p>");
        }
        out.println("<input type=\"submit\"/>");
        out.println("</form>");
        out.println("</div>");
    }

    private List<ArticleCategory> getListAll() {
        CategoryUtils utils = new CategoryUtils(EMF.getEM());
        return utils.listAll();
    }

    private void insert(String categoryName) throws DataIOException {
        CategoryUtils utils = new CategoryUtils(EMF.getEM());
        utils.insert(categoryName);

    }

    private void update(String newcategoryName, String oldcategoryName) throws DataIOException {
        CategoryUtils utils = new CategoryUtils(EMF.getEM());
        utils.update(oldcategoryName, newcategoryName);
    }

    private void delete(String delcategoryName) throws DataIOException {
        CategoryUtils utils = new CategoryUtils(EMF.getEM());
        utils.delete(delcategoryName);

    }

    
}
